package org.gary.base.action;

import static lengreen.util.constant.Constant.DateFomaterString.YYYY_MM_DD_HH_MM_SS;
import static org.gary.core.util.Utils.*;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.gary.base.model.Online;
import org.gary.base.service.OnlineService;
import org.gary.core.service.InitService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author lengreen
 * 
 */
@Component
@Scope("prototype")
public class OnlineAction extends ActionSupport implements
		ServletResponseAware, SessionAware {
	private static final long serialVersionUID = -1520130061367054089L;
	private HttpServletResponse response;
	private Map<String, Object> session;

	private OnlineService onlineService;
	private InitService initService;
	private Online online;
	private List<Online> onlineList;
	/**分页开始页*/
	private int start;
	/**分页最大记录数*/
	private int limit;
	/**是否可见*/
	private String visible;
	/**查找条件qq*/
	private String qq;
	/**查找开始时间*/
	private String fromDate;
	/**查找结束时间*/
	private String toDate;
	/**删除模式*/
	private String mode;
	/**返回结果字符串*/
	private String result;

	/**
	 * 添加一个在线客服
	 * @throws IOException
	 */
	public void add() throws IOException {
		try {
			if (online != null) {
				onlineService.save(online);
				initService.minQQ(session);
				result = "{'success':'true'}";
			} else {
				result = "{'success':'false'}";
			}
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 列出在线客服
	 * @throws IOException
	 */
	public void list() throws IOException {
		start = parseIndex(start, limit);
		Map<String, Object> where = new HashMap<String, Object>();
		where.put(" entity.visible = ", new Boolean(getVisible()));
		setOnlineList(onlineService.list(Online.class, getStart(), getLimit(),
				where, new Object[] { " entity.id desc " }));

		for (Online p : onlineList) {
			System.out.println("ooo-->" + p);
		}

		JsonConfig config = makeDateFormateJsonConfig(YYYY_MM_DD_HH_MM_SS);
		config.setExcludes(new String[] { "visible" });
		JSONArray onlineListJsonArray = JSONArray.fromObject(onlineList, config);
		JSONObject onlineJsonObject = new JSONObject();
		onlineJsonObject.put("onlineTotal", onlineService
				.selectTotalCount(null));
		onlineJsonObject.put("onlineList", onlineListJsonArray);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(onlineJsonObject.toString());
	}

	/**
	 * 搜索在线客服
	 * @throws IOException
	 */
	public void search() throws IOException {
		try {
			start = parseIndex(start, limit);
			Map<String, Object> where = new HashMap<String, Object>();
			where.put(" entity.visible = ", new Boolean(getVisible()));

			System.out.println("qq-->" + qq);
			System.out.println("fromDate-->" + getFromDate());
			System.out.println("toDate-->" + getToDate());

			if (!"".equals(qq.trim())) {
				where.put(" entity.qqNum like ", qq);
			}
			if (!"".equals(getFromDate().trim())) {
				if ("".equals(getToDate().trim())) {
					where.put(" entity.createDate between ", getFromDate()
							+ " 00:00:00' and '"
							+ getFormatDate(new Date(), YYYY_MM_DD_HH_MM_SS));
				} else {
					where.put(" entity.createDate between ", getFromDate()
							+ " 00:00:00' and '" + getToDate() + " 23:59:59");
				}
			}
			setOnlineList(onlineService.list(Online.class, getStart(),
					getLimit(), where, new Object[] { " entity.id desc " }));

			JsonConfig config = makeDateFormateJsonConfig(YYYY_MM_DD_HH_MM_SS);
			config.setExcludes(new String[] { "visible" });
			JSONArray onlineListJsonArray = JSONArray.fromObject(onlineList, config);
			JSONObject onlineJsonObject = new JSONObject();
			onlineJsonObject.put("onlineTotal", onlineService
					.selectTotalCount(null));
			onlineJsonObject.put("onlineList", onlineListJsonArray);
			System.out.println("onlineJsonObject-->"
					+ onlineJsonObject.toString());
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(onlineJsonObject.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 加载一个在线客服通过id
	 * @throws IOException
	 */
	public void load() throws IOException {
		Online p = onlineService.load(Online.class, online.getId());
		JsonConfig config = makeDateFormateJsonConfig(YYYY_MM_DD_HH_MM_SS);
		config.setExcludes(new String[] { "createDate", "id", "visible" });
		JSONObject onlineJsonObject = JSONObject.fromObject(p,
				makeDateFormateJsonConfig(YYYY_MM_DD_HH_MM_SS));
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(onlineJsonObject.toString());
	}

	/**
	 * 更新一个在线客服
	 * @throws IOException
	 */
	public void update() throws IOException {
		if (online != null) {
			onlineService.update(online);
			initService.minQQ(session);
			result = "{'success':'true'}";
		} else {
			result = "{'success':'false'}";
		}
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(result);
	}

	/**
	 * 删除一个在线客服
	 * @throws IOException
	 */
	public void delete() throws IOException {
		try {
			if ("delete".equals(getMode())) {
				onlineService.delete(online);
				initService.minQQ(session);
				result = "{'result':'delete'}";
			} else if ("update".equals(getMode())) {
				onlineService.update(Online.class, new Object[] { online
						.getId() }, false);
				initService.minQQ(session);
				result = "{'result':'update'}";
			} else {
				result = "{'result':'none'}";
			}
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 回收一个在线客服
	 * @throws IOException
	 */
	public void recover() throws IOException {
		try {
			String result = "";
			if (online != null) {
				onlineService.update(Online.class, new Object[] { online
						.getId() }, true);
				initService.minQQ(session);
				result = "{'success':'true'}";
			} else {
				result = "{'success':'false'}";
			}
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getStart() {
		return start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getLimit() {
		return limit;
	}

	public void setVisible(String visible) {
		this.visible = visible;
	}

	public String getVisible() {
		return visible;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public String getMode() {
		return mode;
	}

	public void setOnline(Online online) {
		this.online = online;
	}

	public Online getOnline() {
		return online;
	}

	public void setOnlineList(List<Online> onlineList) {
		this.onlineList = onlineList;
	}

	public List<Online> getOnlineList() {
		return onlineList;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getQq() {
		return qq;
	}

	@Resource
	public void setOnlineService(OnlineService onlineService) {
		this.onlineService = onlineService;
	}

	public OnlineService getOnlineService() {
		return onlineService;
	}

	@Resource
	public void setInitService(InitService initService) {
		this.initService = initService;
	}

	public InitService getInitService() {
		return initService;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}
}
